const app = getApp();
const api = require('../../utils/api');

Page({
    data: {
        navBarHeight: 0,

        userInfo: '', //用户信息
        hasRegisterPhone: false, //是否已经授权电话
        hasBindStore: false, //是否已经绑定店铺
        
        pointAccount: 0, //积分账户
        accountWalletNumber: 0, //店铺会员账户个数
        
        orderWaitForPayNumber: 0, //待支付订单数量
        orderWaitForPickupNumber: 0, //待提货订单数量

        categoryList: [], //平台商品分类
    },
    onLoad(options) {
        const systemInfo = wx.getWindowInfo();
        this.setData({
            navBarHeight: systemInfo.statusBarHeight + 44,
        });
    },
    onShow() {
        this.getUserInfo();
    },
    getUserInfo() { //获取用户信息（包括购物券余额）
        api.post('/user/info')
            .then(res => {
                wx.setStorageSync('userinfo', res);
                app.globalData.userInfo = res;

                let data = res;
                if(data.phone) {
                    data.phone = res.phone ? app.filterPhone(res.phone) : res.phone;
                }
                this.setData({
                    userInfo: data,
                    hasRegisterPhone: data.phone ? true : false,
                    // hasBindStore: data.bindShopId ? true : false, //暂时不用
                });

                if(this.data.hasRegisterPhone) {
                    this.getPointAccount();
                    this.getMemberAccount();
                    this.getWaitForPayOrder();
                    this.getWaitForPickupOrder();
                }
                // if(this.data.hasBindStore) {
                //     this.getCategoryList();
                // }
            })
    },
    getPointAccount() { //获取积分账户余额
        api.post(`/user/member/account`)
            .then(res => {
                this.setData({
                    pointAccount: res.balance
                });
            })
    },
    getMemberAccount() { //获取会员充值账户
        api.post('/user/account/page')
            .then(res => {
                this.setData({
                    accountWalletNumber: res.total
                })
            })
    },

    getWaitForPayOrder() { //获取待支付订单数据
        let params = {
            orderStatus: 1, //订单状态：1.待付款
            pageNum: 1,
            pageSize: 999,
        }
        api.post('/order/page', params)
            .then(res => {
                let list = res.content;
                this.setData({
                    orderWaitForPayNumber: list.length
                })
            })
    },
    getWaitForPickupOrder() { //获取待提货订单数据
        let params = {
            orderStatus: 2, //订单状态：2.已付款
            pageNum: 1,
            pageSize: 999,
        }
        api.post('/order/page', params)
            .then(res => {
                let list = res.content;
                this.setData({
                    orderWaitForPickupNumber: list.length
                })
            })
    },
    getCategoryList() { //获取平台商品分类(店铺采购用)(已停用)
        api.post('/index/platform/category/list')
            .then(res => {
                this.setData({
                    categoryList: res
                });
            })
    },

    gotoLogin() { //去登录页(授权手机)
        wx.navigateTo({
            url: '/pages/login/login?dir=1',
        });
    },
    gotoProfile() { //去个人信息
        wx.navigateTo({
            url: '/pages/userInfo/userInfo',
        });
    },
    gotoSetting() { //去设置
        wx.navigateTo({
            url: '/pages/setting/setting',
        });
    },
    gotoOrder(e) { //去查看订单
        let type = e.currentTarget.dataset.type;
        let url = '/pages/order/order';
        if(type) {
            url = url + '?type=' + type;
        }
        wx.navigateTo({
            url: url,
        });
    },
    gotoCouponRecord() { //查看购券记录
        wx.navigateTo({
            url: '/pages/couponOrder/couponOrder',
        });
    },
    gotoBuyCoupon() { //去购券
        wx.navigateTo({
            url: '/pages/coupon/coupon',
        });
    },
    gotoExchangeCoupon() { //去兑券
        wx.navigateTo({
            url: '/pages/couponExchange/couponExchange',
        });
    },
    gotoMemberAccount() { //去店铺会员账户
        wx.navigateTo({
            url: '/pages/memberAccount/memberAccount',
        });
    },
    gotoCategory(e) { //去平台商品分类列表
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: `/pages/indexCategoryProduct/indexCategoryProduct?saleChannel=1&categoryId=${item.id}&categoryName=${item.categoryName}`,
        });
    },
    gotoCashier() { //测试线下支付
        wx.navigateTo({
            url: '/pages/cashierBySelfService/cashierBySelfService?shopId=19',
        });
    },

    onPullDownRefresh() {
        //刷新页面数据
        this.getUserInfo();
        this.getPointAccount();
        this.getWaitForPayOrder();
        this.getWaitForPickupOrder();
    },
})